#!/usr/bin/python

ranks="23456789TJQKA";
suits="DHCS";


def parseCard(c):
	c=list(c);
	return ( ranks.find(c[0])+1, suits.find(c[1]));

def scoreHand(hand):
	hand=sorted(hand, key=lambda x: x[0]);
	suitcount=[0]*4;
	rankcount=[0]*(len(ranks)+1);

	straight=0;
	lastrank=-1;

	for card in hand:
		if( card[0]==lastrank+1):
			straight+=1;
		else:
			straight=0;
		lastrank=card[0];
		suitcount[card[1]]+=1;
		rankcount[card[0]]+=1;

	if(straight==4 and suitcount.count(5)==1): #straight flush
		if( (hand[0])[4]==len(ranks) ):
			return 10**10;
		return 10**9;
	
	if( rankcount.count(4)==1):# four of a kind
		return  (rankcount.index(4))*(10**7);

	if( rankcount.count(3)==1 and rankcount.count(2)==1): #full house
		return 10**6 + 100*rankcount.index(3) +rankcount.index(2);

	if( suitcount.count(5)==1): #flush
		return 10**5;
	
	if( straight==4 ): #straight
		return 10**4;

	if( rankcount.count(3)==1 ): #three of a kind
		return 10**3+rankcount.index(3);

	if( rankcount.count(2)==2 ):
		score=10**2+rankcount.index(2);
		rankcount[rankcount.index(2)]==0;
		return score+ 20*rankcount.index(2);

	if( rankcount.count(2)==1 ): #one pair
		return 50+rankcount.index(2);
	
	return (hand[4])[0];#highest value card
	
def breaktie(hand1, hand2): # if > 0,  hand 1 wins
	hand1=sorted(hand1, key=lambda x: x[0]);
	hand2=sorted(hand2, key=lambda x: x[0]);
	for i in xrange(5):
		if( (hand1[4-i])[0] != (hand2[4-i])[0]):
			return (hand1[4-i])[0]-(hand2[4-i])[0];
	return 0;
	
	
	

def euler54():
	handfile=open("poker.txt", "r");
	
	wincount=0;

	for line in handfile:
		rawcards=line.split(" ");
		hand1=map(parseCard, rawcards[0:5]);
		hand2= map(parseCard, rawcards[5:]);
		p1score=scoreHand( hand1);
		p2score=scoreHand( hand2);
		
		if(p1score>p2score):
			wincount+=1;
			#print "p1 wins";
		elif( p1score==p2score and breaktie(hand1, hand2)>0):
			wincount+=1;
			#print "p1 wins";
		else:
			#print "p2 wins";
			pass;

	return wincount;


print euler54();
